<?php


namespace app\admin\controller\chat;


use app\admin\model\Duty;
use app\admin\model\Reply;
use app\admin\model\ReplyInfo;
use app\admin\model\Staff;
use app\common\Common;
use think\facade\Cache;
use think\Request;

class FastReply extends Common
{
    // 我的消息添加
    public function addMy(Request $request)
    {
        $info = $request->param("info");
        if(!$info)return failedJson("消息不能为空~");
        $user = $request->_user;
        $res = Reply::addReplyIn($info,$user["staff_no"],$user["duty_id"]);
        $redis = Cache::store('default')->handler();
        Reply::setNewInfo($redis,$user["staff_no"],1);
        if($res)return failedJson($res);
        return successJson("success","成功");
    }

    // 列表
    public function treeList(Request $request)
    {
        $user = $request->_user;
        $dutyIdArr = Duty::getDataDepartment();
        $res = Reply::treeList($dutyIdArr,$user["staff_no"]);
        return successJson("success",$res);
    }

    // 我的消息修改
    public function upMy(Request $request)
    {
        $id = $request->param("id");
        $info = $request->param("info");
        if(!$info||!$id)return failedJson("消息不能为空~");
        $user = $request->_user;
        $res = Reply::upReplyIn($id,$info,$user["staff_no"]);
        if($res)return failedJson($res);
        $redis = Cache::store('default')->handler();
        Reply::setNewInfo($redis,$user["staff_no"],1);
        return successJson("success","成功");
    }

    // 我的消息删除
    public function delMy(Request $request)
    {
        $id = $request->param("id");
        if(!$id)return failedJson("消息不能为空~");
        $user = $request->_user;
        $res = Reply::delReplyIn($id,$user["staff_no"]);
        if($res)return failedJson($res);
        $redis = Cache::store('default')->handler();
        Reply::setNewInfo($redis,$user["staff_no"],1);
        return successJson("success","成功");
    }

    // 我的消息自动
    public function autoMy(Request $request)
    {
        $id = $request->param("id");
        $auto = $request->param("auto");
        if(!in_array($auto,[1,0]))return failedJson("auto参数不正确~");
        if(!$id)return failedJson("消息不能为空~");
        $user = $request->_user;
        $res = Reply::autoReplyIn($id,$user["staff_no"],$auto);
        if($res)return failedJson($res);
        $redis = Cache::store('default')->handler();
        Reply::setNewInfo($redis,$user["staff_no"],1);
        return successJson("success","成功");
    }

    // 自动回复设置
    public function replyAutoSet(Request $request)
    {
        $is_reply_auto = $request->param("is_reply_auto",0);
        $reply_auto_time = $request->param("reply_auto_time",5);
        if($reply_auto_time > 300)return failedJson("不能大于300秒~");
        $user = $request->_user;
        Staff::replyAutoSet($user["staff_no"],$is_reply_auto,$reply_auto_time);
        return successJson("success","成功");
    }
}